$(function(){
    $('#elasticsearch-page').height(window.screen.height * 0.8);
    var identify = localStorage.getItem('ela_identify');
    if (identify == '' || identify == undefined) {
        localStorage.setItem("ela_identify", $('#noncestr').val());
        identify = $('#noncestr').val();
    }

    var elasticsearch_page = new Vue({
        el: "#elasticsearch-page",
        data: {
            content: "基于输入内容，进行分词，统计词频",
            noncestr: '',
            dataList: [],
            hotPhrase: [],
        },
        mounted: function() {
            this.noncestr = $('#noncestr').val();
            this.initialList();
        },
        methods: {
            // 获取列表数据
            initialList: function() {
                this.$http
                    .post('listElasticsearch', {noncestr: this.noncestr, ela_identify: identify})
                    .then(
                        function (res) {
                            if (typeof res.body == 'object') {
                                var response = res.body;
                            } else {
                                var response = JSON.parse(res.body);
                            }

                            if (response.responseCode == 200) {
                                this.dataList = [];
                                this.hotPhrase = [];
                                for (var i = 0; i < response.data.mongoData.length; i++) {
                                    this.dataList.push(response.data.mongoData[i]);
                                }

                                for (var i = 0; i < response.data.hotPhraseData.length; i++) {
                                    this.hotPhrase.push(response.data.hotPhraseData[i]);
                                }
                            } else {
                                new NoticeJs({
                                    text: response.message,
                                    position: 'topCenter',
                                    type: 'error',
                                    modal: true,
                                    animation: {
                                        open: 'animated bounceInRight',
                                        close: 'animated bounceOutLeft'
                                    }
                                }).show();
                            }
                        },
                        function (res) {
                            new NoticeJs({
                                text: '服务端错误',
                                position: 'topCenter',
                                type: 'error',
                                modal: true,
                                animation: {
                                    open: 'animated bounceInRight',
                                    close: 'animated bounceOutLeft'
                                }
                            }).show();
                        });
            },
            // 提交搜索
            search: function () {
                if (this.content == '') {
                    new NoticeJs({
                        text: '内容不能为空',
                        position: 'topCenter',
                        type: 'error',
                        modal: true,
                        animation: {
                            open: 'animated bounceInRight',
                            close: 'animated bounceOutLeft'
                        }
                    }).show();
                    return false;
                }

                this.$http
                    .post('addElasticsearch', {content: this.content, ela_identify: identify})
                    .then(
                        function (res) {
                            if (typeof res.body == 'object') {
                                var response = res.body;
                            } else {
                                var response = JSON.parse(res.body);
                            }

                            if (response.responseCode == 200) {
                                this.content = '';
                                new NoticeJs({
                                    text: '添加成功',
                                    position: 'topCenter',
                                    type: 'success',
                                    modal: true,
                                    timeout: 20,
                                    animation: {
                                        open: 'animated bounceInRight',
                                        close: 'animated bounceOutLeft'
                                    }
                                }).show();

                                setTimeout(function () {
                                    elasticsearch_page.initialList();
                                }, 2000);
                            } else {
                                new NoticeJs({
                                    text: response.message,
                                    position: 'topCenter',
                                    type: 'error',
                                    modal: true,
                                    animation: {
                                        open: 'animated bounceInRight',
                                        close: 'animated bounceOutLeft'
                                    }
                                }).show();
                            }
                        },
                        function (res) {
                            new NoticeJs({
                                text: '服务端错误',
                                position: 'topCenter',
                                type: 'error',
                                modal: true,
                                animation: {
                                    open: 'animated bounceInRight',
                                    close: 'animated bounceOutLeft'
                                }
                            }).show();
                        });
            }
        }
    });
});